package com.ct.gd.passport.view.passport;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.domain.PassportAppInfo;
import com.ct.gd.passport.view.AbsCatalogFragment;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.content.SimpleLoader;
import com.up91.common.android.helper.ToastHelper;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class PassportAppHistoryFragment extends AbsCatalogFragment<PassportAppInfo> {

	@Override
	protected void beforeLoad(Loader<List<PassportAppInfo>> loader) {
	}

	@Override
	protected SimpleListAdapter<PassportAppInfo> onInitListAdapter() {
		AppHistoryAdapter adapter = new AppHistoryAdapter(getActivity(), null);
		return adapter;
	}

	@Override
	public Loader<List<PassportAppInfo>> onCreateLoader(int id, Bundle args) {
		try {
			return new PassportAppHistoryLoader(getActivity(), getListView());
		} catch (Exception e) {
			ToastHelper.toast(getActivity(), "暂无表单记录");
			return null;
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		PassportAppInfo info = (PassportAppInfo) l.getItemAtPosition(position);
		if(info!=null){
			MobclickAgent.onEvent(super.getActivity(), UmengVar.PASSPORT_MY_DETAIL);
			String hId = ((TextView)v.findViewById(R.id.passport_app_history_item_id)).getText().toString();
			Intent intent = new Intent(this.getActivity(), PassportHistoryDetailActivity.class);
			intent.putExtra("id", info.getId());
			startActivity(intent);
		}
//		new QueryPassportTask(this.getLoaderManager().getLoader(getLevel())).execute(info.getId());
	}

	private static class AppHistoryAdapter extends SimpleListAdapter<PassportAppInfo> {
		private class ViewHolder {
			TextView tvName;
			TextView tvDate;
			TextView tvArea;
			TextView tvId;
		}

		public AppHistoryAdapter(Context context, List<PassportAppInfo> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.passport_app_history_item, null);
				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.passport_app_history_item_name);
				holder.tvDate = (TextView) convertView.findViewById(R.id.passport_app_history_item_date);
				holder.tvArea = (TextView) convertView.findViewById(R.id.passport_app_history_item_area);
				holder.tvId = (TextView) convertView.findViewById(R.id.passport_app_history_item_id);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			PassportAppInfo t = getData().get(position);
			holder.tvName.setText(t.getName());
			holder.tvDate.setText(t.getDate());
			holder.tvArea.setText("HKG".equals(t.getGoArea())?"香港":"澳门");
			holder.tvId.setText(t.getId());
			// holder.tvName.setText(t.getName());
			// holder.tvDate.setText(t.getDate());
			// holder.tvArea.setText(t.getGoArea());
			return convertView;
		}
	}

	private static class PassportAppHistoryLoader extends SimpleLoader<List<PassportAppInfo>> {

		private int	pageSize	= 99;
		private int	pageIndex	= 0;
		
		public PassportAppHistoryLoader(Context context, View view) {
			super(context, view);
		}

		@Override
		protected List<PassportAppInfo> loadData() throws Exception {
//			return PassportRecordFacade.instance.getPassportRecordList();
			return PassportAppInfo.load();
		}
	}
	


}
